/* 前缀和与差分
* 
* 本题:
    矩阵前缀和
*/

#define DEBUG
#pragma GCC optimize("O1,O2,O3,Ofast")
#pragma GCC optimize("no-stack-protector,unroll-loops,fast-math,inline")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,ssse3")

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 5010;

int s[N][N];

signed main()
{
    #ifdef DEBUG
        freopen("./in.txt", "r", stdin);
    #else
        ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #endif
    
    int n, R;
    cin >> n >> R;
    R = min(R, 5001);
    for(int i = 0; i < n; i++)
    {
        int x, y, w; cin  >> x >> y >> w;
        x++; y++; s[x][y] += w; //边界优化
    }

    //前缀和处理
    for(int i = 1; i <= 5001; i++)
        for(int j = 1; j <= 5001; j++)
            s[i][j] += s[i-1][j] + s[i][j-1] - s[i-1][j-1];

    int res = 0;
    for(int i = R; i <= 5001; i++)
        for(int j = R; j <= 5001; j++)
            res = max(res, s[i][j] - s[i-R][j] - s[i][j-R] + s[i-R][j-R]);
    
    cout << res << endl;
    return 0;
}